<template>
  <div class="fillcontain">
    <head-top></head-top>
    <div class="table_container">
      <el-button
        type="primary"
        @click="handleAdd">添加</el-button>
      <!--列表-->
      <el-table :data="DList" highlight-current-row v-loading="loading" style="width: 100%;">
        <el-table-column type="index">
        </el-table-column>
        <el-table-column prop="serialNum" label="终端编号" sortable>
        </el-table-column>
        <el-table-column prop="IP" label="ip地址" sortable>
        </el-table-column>
        <el-table-column prop="MAC" label="MAC地址" sortable>
        </el-table-column>
        <el-table-column label="状态" sortable>
          <template slot-scope="scope">
            <span style="margin-left: 10px" v-if="!scope.row.status">开机</span>
            <span style="margin-left: 10px" v-else>关机</span>
          </template>
        </el-table-column>
        <!--<el-table-column prop="age" label="name数量" width="100" sortable>-->
        <!--</el-table-column>-->
        <el-table-column prop="station" label="工位" sortable>
        </el-table-column>
      </el-table>
      <el-col :span="24" class="toolbar">
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
        </el-pagination>
      </el-col>

      <el-dialog title="添加监控终端" :visible.sync="dialogFormVisible" size="tiny">
        <el-form :model="selectTable">
          <el-form-item label="终端序列号" label-width="100px">
            <el-input v-model="selectTable.serialNum" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="IP地址" label-width="100px">
            <el-input v-model="selectTable.IP"></el-input>
          </el-form-item>
          <el-form-item label="MAC" label-width="100px">
            <el-input v-model="selectTable.MAC"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="createTerm">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import headTop from './head.vue'
  //  import {baseUrl, baseImgPath} from '@/config/env'
  import {terminalList, createTerminalList} from '@/api/api'
  export default {
    data () {
      return {
        selectTable: {
          serialNum: '',
          IP: '',
          MAC: ''
        },
        dialogFormVisible: false,
        DList: [],
        page: 1,
        total: 0
      }
    },
    components: {
      headTop
    },
    created () {
      this.initData()
    },
    methods: {
      // 获取设备列表
      async initData () {
        this.loading = true
        this.selectTable = {
          serialNum: '',
          IP: '',
          MAC: ''
        } // 重置输入框内容
        try {
          terminalList(this.page).then((res) => {
            this.total = res.data.count
            this.DList = res.data.results
            this.loading = false
          })
        } catch (err) {
          console.log('获取数据失败', err)
        }
      },
      handleCurrentChange (val) {
        this.page = val
        this.initData()
      },
      handleAdd () {
        this.dialogFormVisible = true
      },
      async createTerm () {
        createTerminalList(this.selectTable).then(res => {
          this.$message({
            type: 'success',
            message: '添加成功'
          })
          this.dialogFormVisible = false
          this.loading = false
          this.initData()
        })
      }
    }
  }
</script>

<style scoped lang="less">
  .el-button {
    margin: 5px;
  }
</style>
